package logic

import (
	"beike/houserpc/houserpc"
	"beike/houserpc/internal/svc"
	"beike/models"
	"context"
	"fmt"
	"strings"

	"github.com/zeromicro/go-zero/core/logx"
)

type AskAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAskAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AskAddLogic {
	return &AskAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AskAddLogic) AskAdd(in *houserpc.AskAddRequest) (*houserpc.AskAddResponse, error) {
	// todo: add your logic here and delete this line

	var user models.User

	models.DB.Debug().Where("id = ?", in.UserId).Find(&user)
	if user.ID == 0 {
		return nil, fmt.Errorf("当前用户不存在")
	}

	var house models.House

	models.DB.Debug().Where("id = ?", in.HouseId).Find(&house)
	if user.ID == 0 {
		return nil, fmt.Errorf("当前房源不存在")
	}

	var ask = models.Ask{
		UserId:  in.UserId,
		HouseId: in.HouseId,
		Content: in.Content,
	}

	if strings.Contains(in.Content, "中介费") || strings.Contains(in.Content, "虚假") {
		return nil, fmt.Errorf("当前问题中涉及敏感词汇，请重新提问")
	}

	models.DB.Debug().Create(&ask)

	return &houserpc.AskAddResponse{
		AskId: int64(ask.ID),
	}, nil
}
